package butines.core.gui;

import java.awt.Graphics2D;

import butines.core.GameImage;

public class GUIImage extends GUIComponent {

	private GameImage image;
	private GameImage imageHovered;
	private GameImage imageDisabled;
	private GameImage imageActived;
	
	public GUIImage(int x, int y) {
		setPosition(x, y);
	}

    protected GameImage getCurrentImage() {
	    if (!isEnabled()) {
	        return (imageDisabled == null) ? image : imageDisabled;
        } else  if (isPressed()) {
            return  (imageActived == null) ? image : imageActived;
        } else if (isHovered()) {
            return (imageHovered == null) ? image : imageHovered;
        }
	    return image;
    }

    @Override
    protected void renderComponent(Graphics2D g2d) {
        GameImage currentImage = getCurrentImage();
        if (currentImage != null) {
            currentImage.draw(g2d, getX(), getY());
        }
        measuring(currentImage);
    }

    private void measuring(GameImage img) {
        if (img != null && (getWidth() == 0 || getHeight() == 0)) { 
            setSize(img.getWidth(), img.getHeight());
        }
    }
    
    public void setImageActived(GameImage imageSet, int ox, int oy, int w, int h) {
		this.imageActived = imageSet.clippedCopy(ox, oy, w, h);
	}

	public void setImageDisabled(GameImage imageSet, int ox, int oy, int w, int h) {
		this.imageDisabled = imageSet.clippedCopy(ox, oy, w, h);
	}

	public void setImageHovered(GameImage imageSet, int ox, int oy, int w, int h) {
		this.imageHovered = imageSet.clippedCopy(ox, oy, w, h);
	}

	public void setImage(GameImage imageSet, int ox, int oy, int w, int h) {
		this.image = imageSet.clippedCopy(ox, oy, w, h);
	}

    public GameImage getImageHovered() {
        return imageHovered;
    }

    public void setImageHovered(GameImage hoveredImage) {
        this.imageHovered = hoveredImage;
    }

    public GameImage getImageDisabled() {
        return imageDisabled;
    }

    public void setImageDisabled(GameImage disabledImage) {
        this.imageDisabled = disabledImage;
    }

    public GameImage getImageActived() {
        return imageActived;
    }

    public void setImageActived(GameImage activedImage) {
        this.imageActived = activedImage;
    }

    public void setImage(GameImage defaultImage) {
        this.image = defaultImage;
    }

}
